/*
* INSANE - Interactive Structural Analysis Environment
*
* Copyright (C) 2003-2005
* Universidade Federal de Minas Gerais
* Escola de Engenharia
* Departamento de Engenharia de Estruturas
* 
* Author's email :    insane@dees.ufmg.br
* Author's website :  http://www.dees.ufmg.br/insane
* 
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or any later version.
* 
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/
package br.ufmg.dees.insane.ui.rich.full.command;

import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.ListIterator;

import javax.swing.JInternalFrame;

import br.ufmg.dees.insane.commons.command.Command;
import br.ufmg.dees.insane.commons.controller.Controller;
import br.ufmg.dees.insane.commons.view.DrawingArea;
import br.ufmg.dees.insane.model.geo.GLine;
import br.ufmg.dees.insane.model.geo.GPoint;
import br.ufmg.dees.insane.ui.rich.full.InsaneInternalInterface;

/**
 * A command to add many lines using the mouse to enter its coords.
 * @author Fonseca, Flavio & Pitangueira, Roque
 * @since January 2004
 */
public class AddPolylineMouseCommand implements Command, Cloneable {
    
    private InsaneInternalInterface parent;
    private Controller controller;
    private DrawingArea da;
    private GPoint ini;
    private GPoint fin;
    private GLine l;
	private ArrayList<Object> newPoints = new ArrayList<Object>();
    
//*****************************************************************************
	
    /** Constructor method. */
    public AddPolylineMouseCommand(JInternalFrame parent) {
        this.parent = (InsaneInternalInterface)parent;
        controller = this.parent.getController();
        da = this.parent.getDrawing();
    }
    
//*****************************************************************************
	
	/** The execute method.*/
public void execute() {
        
		Point2D po = da.getWorldPoint();
        
        if (ini == null){
            if (!controller.contains(po)){
				GPoint p2 = new GPoint(po);
				controller.add(p2);
				da.repaint();
				newPoints.add((GPoint)controller.get(po));
				ini = (GPoint)controller.get(po);
                parent.addOutputText(parent.getResourceBundle().getString("cmIniPointCreated") + " " + ini.getX() + ", " + ini.getY());
            }else{
                ini = (GPoint)controller.get(po);
                parent.addOutputText(parent.getResourceBundle().getString("cmFirstPointChosen") + " " + ini.getX() + ", " + ini.getY());
            }
            PreviewLineCommand pl = new PreviewLineCommand(parent);
            pl.setInitialPoint(da.getViewPoint(ini));
            da.setMovedCommand(pl);
            da.setRepainting(true);
            da.repaint();
			parent.addOutputText(parent.getResourceBundle().getString("cmSpecNextPoint"));
			parent.getConsole().setLastPoint(new Point2D.Double(ini.getX(), ini.getY()));
        }else{
            if (po.getX()!=ini.getX() || po.getY()!=ini.getY()) {
					if (!controller.contains(po)){
					GPoint p2 = new GPoint(po);
					controller.add(p2);
					da.repaint();
					newPoints.add((GPoint)controller.get(po));
					fin = (GPoint)controller.get(po);
					parent.addOutputText(parent.getResourceBundle().getString("cmPointCreated") + " " + fin.getX() + ", " + fin.getY());
				} else {
					fin = (GPoint)controller.get(po);
					parent.addOutputText(parent.getResourceBundle().getString("cmPointChosen") + " " + fin.getX() + ", " + fin.getY());
				}
				l = new GLine(ini, fin);
				controller.add(l);
				ini = fin;
				PreviewLineCommand pl = new PreviewLineCommand(parent);
				pl.setInitialPoint(da.getViewPoint(ini));
				da.setMovedCommand(pl);
				da.setRepainting(true);
				da.repaint();
				parent.getConsole().setLastPoint(new Point2D.Double(fin.getX(), fin.getY()));
				
				parent.addOutputText(parent.getResourceBundle().getString("cmSpecNextPoint"));
				parent.addToUndo((Command)this.clone());
				newPoints.clear();
			} else {
				parent.addOutputText(parent.getResourceBundle().getString("cmMustBeDifferent"));
				parent.addOutputText(parent.getResourceBundle().getString("cmSpecNextPoint"));
			}
        }
     }
	 
//*****************************************************************************
	 
	 /** The undo method.*/
	 public void undo() {
		 controller.remove(l);
		 ListIterator nPoints = newPoints.listIterator();
		 while (nPoints.hasNext()) {
			 GPoint np = (GPoint) nPoints.next();
			 controller.remove(np);
		 }
	 }
	 
//*****************************************************************************
	
	/** The redo method.*/
    public void redo() {
        ListIterator nPoints = newPoints.listIterator();
		while (nPoints.hasNext()) {
			 GPoint np = (GPoint) nPoints.next();
			 controller.add(np);
		 }
		 controller.add(l);
    }
    
//*****************************************************************************
	
	/** The clone method.*/
    @SuppressWarnings("unchecked")
	public Object clone() {
        try {
            AddPolylineMouseCommand cmd = (AddPolylineMouseCommand)super.clone();
            cmd.newPoints = (ArrayList<Object>) newPoints.clone();
			cmd.l = (GLine) l.clone();
            return cmd;
        }catch (CloneNotSupportedException e) {
            throw new Error("Clonning ERROR - This should never happen!");
        }
    }
	
//*****************************************************************************
	
	/** Adds a GPoint to the newPoints ArrayList.
	*@param a The GPoint to be added.
	*/
	public void addNewPoint(GPoint a) {
		newPoints.add(a);
	}
	
//*****************************************************************************
	
	/** Adds a GLine to the newPoints ArrayList.
	*@param a The GLine to be added.
	*/
	public void addNewLine(GLine a) {
		newPoints.add(a);
	}
//*****************************************************************************
	
	/** Returns the initial point of the line being created.<br>
	* It's used to allow the use of both methods of inputing data (mouse and keyboard).
	* @return The initial point of the line being created.
	*/
	public GPoint getInitialPoint() {
		return ini;
	}
	
//*****************************************************************************
	
	/** Sets the initial point of the line being created.<br>
	* It's used to avoid errors when mixing both methods of inputing data (mouse and keyboard).
	* @param a The initial point of the line being created.
	*/
	public void setInitialPoint(GPoint a) {
		ini = a;
	}
	
//*****************************************************************************
	
	/** Sets the final point of the line being created.<br>
	* It's used to avoid errors when mixing both methods of inputing data (mouse and keyboard).
	*@param a The final point of the line being created.
	*/
	public void setFinalPoint(GPoint a) {
		fin = a;
	}
	
//*****************************************************************************
	
	/** Returns the final point of the line being created.<br>
	* It's used to avoid errors when mixing both methods of inputing data (mouse and keyboard).
	*@return The final point of the line being created.
	*/
	public GPoint getFinalPoint() {
		return fin;
	}
	
//*****************************************************************************
	
	/** Sets the line being created.<br>
	* It's used to avoid errors when mixing both methods of inputing data (mouse and keyboard).
	*@param a The line being created.
	*/
	public void setLine(GLine a) {
		l = a;
	}
	
//*****************************************************************************
	
	/** Returns the line being created.<br>
	* It's used to avoid errors when mixing both methods of inputing data (mouse and keyboard).
	*@return The line being created.
	*/
	public GLine getLine() {
		return l;
	}
	
//*****************************************************************************
	
	/** Clears the newPoints ArrayList.*/
	public void clearNewPoints() {
		newPoints.clear();
	}
	
//*****************************************************************************
}

